package org.biojava.services.das.dao;

import java.net.URL;
import java.sql.SQLException;
import java.util.List;

import org.biojava.dasobert.dasregistry.Das1Validator;
import org.biojava.services.das2.Das2Mirror;

public class ServerManager {

	
	private DasServerDAO serverDao;
	public DasServerDAO getServerDao() {
		return serverDao;
	}

	public void setServerDao(DasServerDAO serverDao) {
		this.serverDao = serverDao;
	}

	public DasUserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(DasUserDAO userDao) {
		this.userDao = userDao;
	}

	private DasUserDAO userDao;

	/**
	 * 
	 * @param sourcesPath path with sources on the end of the url (not the rootUrl of the server)
	 * @param openId
	 * @param description
	 * @return
	 * @throws SQLException
	 */
	public String storeServer(String sourcesPath, String openId, String description){
		String errors="";
		if(sourcesPath.equals("")){
			return "you must have a valid sources Path";
		}
		if((!sourcesPath.endsWith("sources"))&&(!sourcesPath.endsWith("sources/"))){
			return "path must end with sources";
		}
		if(openId.equals("") || openId.equals(null)){
			return "You need to specify an Open ID";
		}
		//work with true server root from now on
		String rootUrl=Das1Validator.removeDataSourceNameFromUrl(sourcesPath);
		//surely we need the business logic here and the DataAccessObjects just do that - access the data whether from a database or flat file or whatever??
		//if result is "" then it is ok and continue
		//if anything else then we have a problem
		boolean exists=serverDao.exists(rootUrl);
		//we don't want to register it if someone owns it already i.e. user is !=0 unless it is the owner in which case we want to update it
		if(exists){
			//RegistryUser user=userDao.getUser(openid);
			int userId=serverDao.getUserIdForServerUrl(rootUrl);
			if (userId==0) {
				//if userId is 0 then fine udate the source for this owner
				errors=serverDao.updateServer(rootUrl, openId, description);
			}else{
				RegistryUser user = userDao.getUser(openId);
				System.out.println(user.getUserID());
				if (userId == user.getUserID()) {
					//update this as the person on this page is the owner of this source
					System.out.println("you are the owner");
					errors=serverDao.updateServer(rootUrl, openId, description);
				} else {
					errors= "This server is registered to someone already - You are not the registered owner of this server and so cannot update it - if you dispute this please contact the registry";
				}
			}
			
		}else{
		//if server not in the registry at all	
		errors=serverDao.storeServerInDb(rootUrl, openId, description);
		}
		//add this server id to all the sources all the sources in the registry table
		
		
		
		return errors;
		
	}

	public List<DasServer> getServers() {
		return serverDao.getDasServers();
	}

	public DasServer getServerForServerUrl(String url) {
		return serverDao.getServerForServerUrl(url);
	}

	public List<DasServer> getDasServers() {
		return serverDao.getDasServers();
	}

	public DasServer getServerIdForServerUrl(String serverRootUrl) {
		return serverDao.getServerForServerUrl(serverRootUrl);
	}
	
	public int getUserIdForServerUrl(String url) {
		return serverDao.getUserIdForServerUrl(url);
	}
	
	
}
